Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },


  getLocation: function () {
    let that = this;
    wx.getSetting({
      success: (res) => {
        console.log(JSON.stringify(res))
        // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
        // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
        // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                console.log("拒绝授权")
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      console.log("授权成功")
                      //再次授权，调用wx.getLocation的API
                      that.getWeather()
                    } else {
                      console.log("授权失败")
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //调用wx.getLocation的API
          that.getWeather()
        } else {
          //调用wx.getLocation的API
          that.getWeather()
        }
      }
    })
  },
  getWeather: function () {
    var BMap = new bmap.BMapWX({
      ak: 'BUMnBXmRXwkQzBH6mj4bdQsN0ie8lbP7'
    });
    var fail = function (data) {
      console.log(data)
    };
    var success = function (data) {
      var weatherData = data.currentWeather[0];
      weatherData = '城市：' + weatherData.currentCity + '\n' + 'PM2.5：' + weatherData.pm25 + '\n' + '日期：' + weatherData.date + '\n' + '温度：' + weatherData.temperature + '\n' + '天气：' + weatherData.weatherDesc + '\n' + '风力：' + weatherData.wind + '\n';
      console.log(weatherData)
    }
    // 发起weather请求 
    BMap.weather({
      fail: fail,
      success: success
    });
  },
  getWeather1: function () {
    wx.getLocation({
      success: function (res) {
        console.log(res)

      },
    })
    wx.request({
      url: 'https://www.tianqiapi.com/api/?version=v6&cityid=101110101&appid=[57645563]&appsecret=[FLI9ENjh]',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
      }
    })
  },
})